package cn.songtx.iot.controller;

import cn.songtx.iot.pojo.Result;
import cn.songtx.iot.pojo.SysUserFamily;
import cn.songtx.iot.service.ISysUserFamilyService;
import cn.songtx.iot.vo.UserFamilyVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/userFamily")
@RequiredArgsConstructor
public class SysUserFamilyController {
    /**
     * 家庭关系不提供修改方法,如要修改直接删除旧关系,创建新关系
     */
    private final ISysUserFamilyService userFamilyService;

    /**
     * 家庭关系:创建用户-家庭关联
     * 查重,创建前先查询是否存在,如不存在则创建
     *
     * @param sysUserId   用户id
     * @param sysFamilyId 家庭id
     * @return 返回状态
     */
    @RequestMapping(value = "/save", method = RequestMethod.GET)
    public Result userFamilySave(Long sysUserId, Long sysFamilyId) {
        LambdaQueryWrapper<SysUserFamily> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserFamily::getUserId, sysUserId)
                .eq(SysUserFamily::getFamilyId, sysFamilyId);
        boolean exists = userFamilyService.exists(lambdaQueryWrapper);
        if (!exists) {
            SysUserFamily sysUserFamily = new SysUserFamily();
            sysUserFamily.setUserId(sysUserId);
            sysUserFamily.setFamilyId(sysFamilyId);
            sysUserFamily.setCreateTime(LocalDateTime.now());
            boolean saved = userFamilyService.save(sysUserFamily);
            return Result.success(saved ? "创建成功" : "创建失败");
        }
        return Result.success("关联已存在");
    }

    /**
     * 家庭关系:根据本表id逻辑删除家庭关系
     *
     * @param id 本表的id
     * @return 返回状态
     */
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.DELETE)
    public Result userFamilyRemove(@PathVariable("id") Long id) {
        boolean removed = userFamilyService.removeById(id);
        return Result.success(removed ? "逻辑删除成功" : "逻辑删除失败");
    }

    /**
     * 家庭关系:逻辑删除familyId的所有家庭
     */
    @RequestMapping(value = "/deleteByFamilyId/{id}", method = RequestMethod.DELETE)
    public boolean useFamilyRemoveByFamilyId(@PathVariable("id") Long id) {
        LambdaQueryWrapper<SysUserFamily> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserFamily::getFamilyId, id);
        return userFamilyService.remove(lambdaQueryWrapper);
    }

    /**
     * 家庭关系:查询具有管理权限的家庭关系,多表查询
     * 1.根据sysUserId查询sysFamily表,返回familyIds
     * 2.根据familyIds查询sysUserFamily表,返回家庭关系
     * 3.数据的解析userId->userName,familyId->familyName
     *
     * @param sysUserId 用户id
     * @return 返回家庭关系
     */
    @RequestMapping(value = "/listByOwner", method = RequestMethod.GET)
    public Result userFamilyListByOwner(Long sysUserId) {
        List<UserFamilyVO> userFamilyVOS = userFamilyService.selectUserFamilyListByOwner(sysUserId);
        return Result.success(userFamilyVOS);
    }

    /**
     * 家庭关系:所有账户可查,查询本账户拥有的家庭列表,单表查询
     */
    @RequestMapping(value = "/listByUser", method = RequestMethod.GET)
    public Result userFamilyListByUser(Long sysUserId) {
        List<UserFamilyVO> sysUserFamilies = userFamilyService.selectUserFamilyListByUser(sysUserId);
        return Result.success(sysUserFamilies);
    }
}
